#include "documenttypedelegate.h"
#include<QComboBox>
DocumentTypeDelegate::DocumentTypeDelegate(QObject*parent) :QStyledItemDelegate(parent){}

QWidget *DocumentTypeDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    //挂到我们改类上
    QComboBox *box = new QComboBox(parent);
    box->addItems({"pdf", "doc", "markdown", "txt"});
    box->setFrame(false);          // 去掉边框更美观
    return box;
}

void DocumentTypeDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    //取出单元格的值
     QString old = index.model()->data(index, Qt::EditRole).toString();
    //editor是我们创建的下拉框
     QComboBox *box = static_cast<QComboBox *>(editor);
    box->setCurrentText(old);
}

void DocumentTypeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    //拿到用户选择的值
    QComboBox *box = static_cast<QComboBox *>(editor);
    QString newType = box->currentText();
    model->setData(index, newType, Qt::EditRole);   // 写回模型
}

void DocumentTypeDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);   // 大小跟单元格一致
}

